/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.awt.Point;
import java.awt.geom.Point2D;
import org.jdesktop.swingx.JXMapViewer;
import org.jdesktop.swingx.mapviewer.GeoPosition;

/**
 * Helper class to get do various operations on/with GPS Positions
 * 
 * @author Pascal
 */
public class MapUtils {
    private MapUtils() { //no need to instantiate
    }
    
    public static GeoPosition getGeoPointByClickEvent(Point point, JXMapViewer map) {
        Point2D p = (Point2D)map.getCenter().clone();
        
        GeoPosition pos = map.convertPointToGeoPosition(point);
        
        return pos;
    }
    
    /**
     * Calculates the distance between to Points
     * 
     * @param p1
     * @param p2
     * @return the distance between p1 and p2
     */
    public static double calcDistance(GeoPosition p1, GeoPosition p2) {
        
        double lat1 = (float)p1.getLatitude();
        double lon1 = (float)p1.getLongitude();
        
        double lat2 = (float)p2.getLatitude();
        double lon2 = (float)p2.getLongitude();
        
        if (lat1 == lat2 && lon1 == lon2) //points are equal skip Math
            return 0.0;
        
        //I know Math Bitch!
        double dist = Math.acos(Math.sin(lat1 * Math.PI / 180) * Math.sin(lat2 * Math.PI / 180) + Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) * Math.cos((lon2 * Math.PI / 180) - (lon1 * Math.PI / 180))) * 6370;
        //Just kidding formula by wikipedia
        
        return dist;
    }
}
